package com.example.cloudmusic

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.cloudmusic.databinding.LayoutBottomSheetBinding
import com.example.cm_player.model.SongInfo
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.example.cm_player.model.PlayListSong
import com.example.cm_player.PlayListAdapter

class MusicListDialog:BottomSheetDialogFragment() {
    private lateinit var binding:LayoutBottomSheetBinding
    var changeMusicCallback: ((SongInfo) -> Unit)? = null
    var removeMusicCallback: ((SongInfo) -> Unit)? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = LayoutBottomSheetBinding.inflate(inflater)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        var cmBottomDialogRecycler = binding.cmBottomDialogRecycler
        val items = mutableListOf<PlayListSong>()
        //TODO 测试
        val songs = listOf(
            SongInfo(
                id = "1",
                title = "苦咖啡·唯一",
                artist = "那奇沃夫 / KKECHO",
                album = "Refrain-Aimer",
                imageUrl = "http://p1.music.126.net/8DkTnzi7jdjWGYl4qbwLCg==/109951164517295956.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://blog-img-bitebyte.oss-cn-chengdu.aliyuncs.com/audio/Ref%EA%9E%89rain-Aimer.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            SongInfo(
                id = "2",
                title = "他只是经过",
                artist = "高旭",
                album = "他只是经过",
                imageUrl = "https://p1.music.126.net/wUog39IHoJb76pL0AVCFNQ==/109951165348116023.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://m704.music.126.net/20250322223347/3e74f07721e3339e0ca8b7f0c443ec23/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/34290289568/a90c/e7d6/a9f6/991328eccc8f3ff75fbb3fda328b728a.m4a",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            SongInfo(
                id = "3",
                title = "晚风",
                artist = "BT07",
                album = "晚风",
                imageUrl = "https://p1.music.126.net/lCblKUB1hLND5FxiVI1_Lw==/109951164919449758.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://m704.music.126.net/20250322223347/3e74f07721e3339e0ca8b7f0c443ec23/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/34290289568/a90c/e7d6/a9f6/991328eccc8f3ff75fbb3fda328b728a.m4a",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            SongInfo(
                id = "4",
                title = "有些",
                artist = "颜人中",
                album = "有些",
                imageUrl = "http://p1.music.126.net/8DkTnzi7jdjWGYl4qbwLCg==/109951164517295956.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://m704.music.126.net/20250322223347/3e74f07721e3339e0ca8b7f0c443ec23/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/34290289568/a90c/e7d6/a9f6/991328eccc8f3ff75fbb3fda328b728a.m4a",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            SongInfo(
                id = "5",
                title = "7%",
                artist = "XMASwu",
                album = "7%",
                imageUrl = "https://p2.music.126.net/YsXfYs7s2-d0lJwRtp_klQ==/109951164700110582.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=1423241987.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            SongInfo(
                id = "6",
                title = "毒药",
                artist = "SN",
                album = "毒药",
                imageUrl = "http://p2.music.126.net/iNiPTo2kIsyytjpZ_gYhBA==/109951168894259186.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=2071596484.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            SongInfo(
                id = "7",
                title = "在一起叫梦",
                artist = "mmm",
                album = "在一起叫梦",
                imageUrl = "http://p1.music.126.net/wNkdszCwZuzZOs-1GoHM4Q==/109951168763488576.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=2023954033.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            SongInfo(
                id = "8",
                title = "独 行侠+爱如潮水",
                artist = "sea蕊",
                album = "独 行侠+爱如潮水",
                imageUrl = "http://p2.music.126.net/ra554YmMoOBw8_8xDiDZNg==/109951166276735546.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=1432968475.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            SongInfo(
                id = "9",
                title = "下坠Falling",
                artist = "刘宗鑫",
                album = "下坠Falling",
                imageUrl = "http://p1.music.126.net/TyQNalBMYwsCOl5sq_NBTA==/109951164573566845.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=1328146041.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            SongInfo(
                id = "10",
                title = "HEAVEN",
                artist = "恩主",
                album = "HEAVEN",
                imageUrl = "http://p1.music.126.net/23n5WHHKF0bI5CmHueAC_w==/109951168657850338.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=2052988645.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            )
        )
        songs.forEach {
            items.add(PlayListSong(it))
        }
        songs.forEach {
            items.add(PlayListSong(it))
        }
        items[5].isShow = true
        //TODO 测试
        val adapter = PlayListAdapter(items)
        adapter.apply {
            removeMusicCallback = {songInfo->
                this@MusicListDialog.removeMusicCallback?.invoke(songInfo)
            }

            changeMusicCallback = {songInfo->
                this@MusicListDialog.changeMusicCallback?.invoke(songInfo)
            }
        }
        cmBottomDialogRecycler.layoutManager = LinearLayoutManager(this.context,RecyclerView.VERTICAL,false)
        cmBottomDialogRecycler.adapter = adapter
        cmBottomDialogRecycler.setHasFixedSize(true)

        binding.cmDialogClose.setOnClickListener {
            dismiss()
        }

    }

}